package com.chance.cc.crawler.development.command.job.domain.du;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.downloader.proxy.Proxy;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.development.command.job.CrawlerJobCommand;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.turnPage;
import static com.chance.cc.crawler.development.command.publish.douyin.DYPublishCrawlerJob.metaServerIP;
import static com.chance.cc.crawler.development.command.publish.douyin.DYPublishCrawlerJob.metaServerPort;

/**
 * @author lt
 * @version 1.0
 * @date 2021-04-28 16:03:28
 * @email okprog@sina.com
 */
public class DUSellCalenderCrawlerSchedulerJob {
    public static final String domainId = "du_app";
    public static final String site = "sell_calender";
    public static final String siteBiz = "sell_calender";
    public static final String startUrl = "https://m.dewu.com/sns/v1/sell/index?v=4.2.0";
    private static CrawlerJobCommand metaServiceCommand = new CrawlerJobCommand(metaServerIP,metaServerPort);
    public static final String kafkaTmpTopic = "tmp_e_commerce";
    public static final String kafkaTopic = "e_commerce";
    public static Proxy proxy = new Proxy();
    static {
        //代理配置
        //H5168QRFNIU3804D
        //5F6B3610BB719FAA
        proxy.setHost("http-dyn.abuyun.com");
        proxy.setPort(9020);
        proxy.setUsername("HL89Q19E86E2987D");
        proxy.setPassword("71F33D94CE5F7BF2");
    }

    public static void main(String[] args) {
//        publishCrawlerScheduleJobInfo();
        System.out.println(crawlerScheduler().generateCrawlerTriggerKey());
    }

    /**
     * 发布任务信息
     * @return
     */
    public static CrawlerJob publishCrawlerScheduleJobInfo(){

        CrawlerJob crawlerJob = crawlerScheduler();

        //发布定时任务
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domainId);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(crawlerJob));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("Du发售日历每日采集任务");
        crawlerScheduleJob.setCrawlerKey(crawlerJob.generateCrawlerKey());
        HttpPage httpPage = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布任务：" + httpPage.getRawText());
        return crawlerJob;
    }

    public static CrawlerJob crawlerScheduler(){

        //文章采集
        CrawlerRequestRecord articleCrawler = doSearchNewsCrawler();

        CrawlerJob crawlerJob = CrawlerJob.builder()
                .triggerInfo(domainId,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-", domainId,site, CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(StringUtils.joinWith("-", "crawler", domainId, site, "queue")))
                .kafkaResultPipeline(null, kafkaTmpTopic, null)
                .fileResultPipeline("","/data/chance_crawler_test/data/du/du_sell_calender.log",false)
                .crawlerJobThreadNumber(20)
                .requestRecord(articleCrawler)
                .build();
        return crawlerJob;
    }

    /**
     * 初始record
     * @return
     */
    public static CrawlerRequestRecord doSearchNewsCrawler(){
        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domainId, turnPage)
                .httpUrl("https://m.dewu.com/sns/v1/sell/index?v=4.2.0")
                .releaseTime(System.currentTimeMillis())
                .needWashed(false)
                .needParsed(true)
                .proxy(proxy)
                .build();

        requestRecord.tagsCreator().bizTags().addDomain(domainId);
        requestRecord.tagsCreator().bizTags().addSite(site);
        requestRecord.tagsCreator().bizTags().addSiteBiz(siteBiz);
        return requestRecord;
    }

}
